const dailyVerseMW = require('express').Router()
dailyVerseMW.route('/wx')
    .post(async (req, res, next) => {
        if (req.jsonData.MsgType[0] == 'text') {
            const { jsonData } = req
            //发送每日经文
            if (jsonData.Content[0].trim() == '每日经文') {
                //新建每日经文会话
                const dailyVerseSession = {
                    sessionType: 'dailyVerse',
                    createDate: require('../../../tools/getDate')('today'),
                    sessionID: jsonData.MsgId[0]
                }
                //在用户数据中添加会话
                const mongoose = require('mongoose')
                const DailyVerse = mongoose.model('dailyVerse', require('../../../model/dailyVerseSchema'))
                const User = mongoose.model('user', require('../../../model/userSchema'))
                
                //重复交验
           
                const repeatData = await User.findOne({userID: req.user.userID})

                if(!repeatData || repeatData?.sessions[repeatData?.sessions.length -1]?.sessionID != jsonData.MsgId[0]){
                    await User.updateOne({ userID: req.user.userID }, { $push: { sessions: dailyVerseSession } })
                }

                //如果数据库没有今天经文就请求
                //获得数据库数据
                // 今天日期
                const today = require('../../../tools/getDate')('today')
                const todayVerse = await DailyVerse.findOne({ verseDate: today })
                if (!todayVerse) {
                    const axios = require('axios')
                    //使用axios来处理请求
                    axios.get(require('../../../config').dailyverseServer).then(response => {

                        //保存数据入库
                        const verse = {
                            content: response.data,
                            verseDate: today
                        }
                        const dailyVerse = new DailyVerse(verse)
                        //防止因网络延迟产生的重复数据
                        DailyVerse.findOne({verseDate: today}).then(isExist=>{
                            // console.log(isExist)
                            if(!isExist) dailyVerse.save()
                        })

                        //保存dailyverse session到数据库
                        const verseSession = {
                            sessionID: jsonData.MsgId[0],
                            userID: req.user.userID,
                            content: jsonData.Content[0],
                            callback: response.data
                        }
                        const VerseSession = mongoose.model('versesession', require('../../../model/dailyVerseSessionSchema'))
                        const newVerseSession = new VerseSession(verseSession)
                             //防止因网络延迟产生的重复数据
                             VerseSession.findOne({sessionID: jsonData.MsgId[0]}).then(isExist=>{
                                if(!isExist) newVerseSession.save()
                            })
                       
                        //使用sendMsg方法发回收到的数据
                        const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], response.data)
                        res.end(xmlSendData)
                    })

                } else {
                    //直接使用数据库数据

                    //保存dailyverse session到数据库
                    const verseSession = {
                        sessionID: jsonData.MsgId[0],
                        userID: req.user.userID,
                        content: jsonData.Content[0],
                        callback: todayVerse.content
                    }
                    const VerseSession = mongoose.model('versesession', require('../../../model/dailyVerseSessionSchema'))

                    const newVerseSession = new VerseSession(verseSession)
                    //防止因网络延迟产生的重复数据
                    VerseSession.findOne({sessionID: jsonData.MsgId[0]}).then(isExist=>{
                        if(!isExist) newVerseSession.save()
                    })

                    //使用sendMsg方法发回收到的数据
                    const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], todayVerse.content)
                    res.end(xmlSendData)
                }

            } else
                next()
        }

    })

module.exports = dailyVerseMW